package com.genesis.web.controller;


import com.genesis.mall.common.PageInfo;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.SmsFlashPromotion;
import com.genesis.mall.service.ISmsFlashPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 限时购表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/flash")
public class SmsFlashPromotionController {
    @Autowired
    private ISmsFlashPromotionService flashPromotionService;

    /**
     * 添加活动
     *
     * @param flashPromotion
     * @return
     */
    @PostMapping("/create")
    private Result createFlash(@RequestBody SmsFlashPromotion flashPromotion) {
        flashPromotion.setEndDate(flashPromotion.getEndDate().substring(0,10));
        flashPromotion.setStartDate(flashPromotion.getStartDate().substring(0,10));
        flashPromotionService.save(flashPromotion);
        return Result.success();
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result deleteFlash(@PathVariable Integer id) {
        flashPromotionService.removeById(id);
        return Result.success();
    }

    /**
     * 根据活动名称分页查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result pageFlash(@RequestParam(required = false) String keyword,
                            @RequestParam(required = false) Integer pageNum,
                            @RequestParam(required = false) Integer pageSize) {
        PageInfo pageInfo = flashPromotionService.pageFlash(keyword, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    /**
     * 修改上`下线状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/update/status/{id}")
    public Result updateStatus(@RequestParam Integer status, @PathVariable Integer id) {
        flashPromotionService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 编辑活动
     *
     * @param flashPromotion
     * @return
     */
    @PostMapping("/update/{id}")
    public Result updateFlash(@RequestBody SmsFlashPromotion flashPromotion) {
        flashPromotionService.updateById(flashPromotion);
        return Result.success();
    }

    /**
     * 获取活动详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getFlashDetails(@PathVariable Integer id) {
        SmsFlashPromotion flashPromotion = flashPromotionService.getById(id);
        return Result.success(flashPromotion);
    }
}
